<?php
declare (strict_types = 1);
namespace app\school\logic\school\admin;

use app\common\exception\LogicException;
use app\admin\logic\system\Log;
use app\school\event\subscribes\school\School as SchoolSubscribes;
use app\school\model\school\admin\SchoolSplitConfig as SchoolSplitConfigModel;
use think\facade\Event;

/**
 * 机构分成配置逻辑层
 */
class SchoolSplitConfig extends SchoolSplitConfigModel
{
    /**
     * 获取列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-21
     * @param    [type]                         $map 条件
     * @return   [type]                              [description]
     */
    public function getList(array $map = [])
    {
        $schoolWhere = $map['school'];
        return $this->hasWhere('school', $schoolWhere)->with(['school'])->order('update_time', 'DESC')->paginate($this->getListRows());
    }

    /**
     * 申请配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-22
     * @param    array                          $data [description]
     * @return   [type]                               [description]
     */
    public function applySplitConfig(array $data)
    {
        // 计算总和
        if (array_sum($data) != 100) {
            throw new LogicException('分成比例总和需为100');

        }

        // 查询当前机构是否有配置
        $config         = $this->scope('mhm')->findOrEmpty();
        $config->value  = $data;
        $config->status = 2;
        $res            = $config->save();

        // 是否成功
        if (!$res) {
            throw new LogicException('申请分成配置失败');
        }

        return $this;
    }

    /**
     * 获取分成配置
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-08-27
     * @return   [type]                         [description]
     */
    public function getSplitConfig()
    {
        return $this->scope('mhm')->findOrEmpty();
    }

    /**
     * 机构分成比例审核操作
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-29
     * @param    int                            $id [description]
     * @return   [type]                             [description]
     */
    public function doExamine(int $id, array $data)
    {
        $config = $this->where('status', 2)->findOrFail($id);

        $this->startTrans();
        try {
            $status = $data['status'] === 1 ? 1 : 0;
            // 审核结果
            $config->status = $status;

            if ($status === 1) {
                // 通过审核
                $body   = '申请的机构分成比例审核已通过';
                $text   = '通过';
            } else {
                $body = '申请的机构分成比例审核未通过，未通过原因【' . $data['reason'] . '】';
                $text = '驳回';
            }
            if ($config->save()) {
                // 操作日志
                Log::write($text . '了机构【' . $config->school->title . '】的分成比例申请', 'school_split', 'examine', true);
                // 通知消息
                Event::subscribe(SchoolSubscribes::class);
                Event::trigger('SchoolSplitExamine', [
                    'admin_user_id' => $config->school->admin_user_id,
                    'body'          => $body,
                ]);
                $this->commit();

                return true;
            }

            throw new LogicException('操作失败');

        } catch (LogicException $e) {
            $this->rollback();
            throw new LogicException('审核失败'.$e->getMessage());

        } catch (\Exception $e) {
            $this->rollback();
            echo $e->getMessage();
            throw new LogicException('审核失败'.$e->getMessage());

        }
    }
}
